package com.wx.could.user.Controller;

import com.wx.could.user.Model.User;
import com.wx.could.user.common.CommonResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("/user")
public class userController {

    List<User> users=new ArrayList();
    @Value("${server.port}")
    private String port;

    @PostConstruct
    private void initUsers(){
        User heliang= User.builder().id(1).name("何亮").age(22).build();
        User tanghe = User.builder().id(2).name("唐何").age(22).build();
        User yangbao = User.builder().id(3).name("杨豹").age(22).build();
        users.add(heliang);
        users.add(tanghe);
        users.add(yangbao);
    }

    @GetMapping("/getAllUser")
    public CommonResult getAllUser(){
        return CommonResult.success(users);
    }

    @GetMapping("/getDailyreportsByUserName")
    public CommonResult getDailyreportsByUserName(@RequestParam String name){
        System.out.println(port);
        Optional<User> any = users.stream().filter(u -> StringUtils.equals(u.getName(), name)).findAny();
        return any.isPresent()?CommonResult.success(any.get().getId()):CommonResult.failed(); //isPresent 是否存在一个对象
    }
}
